﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HZ.MBSM.DBUtility;


public partial class Agency_PriceSwitch : System.Web.UI.Page
{
    public int projectid;
    protected void Page_Load(object sender, EventArgs e)
    {
        //projectid = 1;
        System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> _AccountType = new System.Collections.Generic.List<HZ.MBSM.DAL.AccountType>();
        _AccountType.Add(HZ.MBSM.DAL.AccountType.enumAttorney);
        if (HZ.MBSM.BLL.Login.CheckAssert(_AccountType))
        {
            projectid = SysFun.ToInt(SysFun.IsSQL(Request.QueryString["projectid"]));
            if (Request.Form["Btnopenall"]=="全部开启报价")
            {
                openbjall();
            }
            if (Request.Form["Btncloseall"]=="全部关闭报价")
            {
                closebjall();
            }
            if (Request.Form["B1"] == "开启报价")
            {
                openbj();
            }
            if (Request.Form["B2"]=="关闭报价")
            {
                closebj();
            }
            showdata();
        }
    }


   /// <summary>
   /// datagridview绑定数据
   /// </summary>
    protected void showdata()
    {
        Db db = new Db();
        DbPage dbPage = new DbPage(db);

        string strGetPageLink = "";

        dbPage.Table = "ProjectCompanyAttribution,Company";                                               //表名，可以多个表关联
        dbPage.Key = "Id";
        //该查询可以唯一表示记录的字段，该属性必须设置且只能有一个唯一标识字段
        //        if (!IsPostBack)
        string sql = "";
        if (ComPanyName.Text.Trim().Length > 0)
        {
            sql = "(CompanyName Like '%" + ComPanyName.Text.Trim() + "%' or UserCode Like '%" + ComPanyName.Text.Trim() + "%') and ";
            strGetPageLink = "&CompanyName=" + Server.UrlEncode(SysFun.ToTrim(Request.Form["CompanyName"]));
        }
        dbPage.Where = sql + " projectid=" + projectid + " and ProjectCompanyAttribution.AccountId=Company.AccountId ";

        dbPage.SelectField = "Id,CompanyName,PriceSwitching,UserCode ";                        //可以不设，默认为“*”

        dbPage.OrderBy = "CompanyName";                                //排序字段，可以不输入。不输入时以 Key 作为排序字段

        dbPage.PageIndex = SysFun.ToInt(Request.QueryString["PG"]);             //设置需要查询第几页
        dbPage.PageIndex = SysFun.ToInt(Request["CurrentlyPageIndex"]); //设置需要查询第几页
        dbPage.PageSize = 30;                                                   //设置每页显示多少条记录


        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        //  Response.Write("<p align=center>投标人审核界面</p>");
        sb.Append("<table border=1 cellpadding='0' cellspacing='0' style='border-collapse: collapse' bordercolor='#111111' width='98%'>");
        sb.Append("<tr>");
        sb.Append("<td class='table_title' nowrap>选取</td>");
        sb.Append("<td class='table_title' nowrap>投标人</td>");
        sb.Append("<td  class='table_title' nowrap>投标人注册号</td>");
        sb.Append("<td  class='table_title' nowrap>报价状态</td>");
        //     sb.Append("<td nowrap align=center>选择</td>");
        sb.Append("</tr>");

        int intNum = 0, i, j = 0;
        string zhuangtai = "";//zhuangtai 表示审核状态

        System.Data.IDataReader dr = dbPage.ExecuteReader();
        while (dr.Read())
        {
            intNum++;
            i = SysFun.ToInt(dr["PriceSwitching"]);
            if (i == 0)
            {
                zhuangtai = "关";
            }
            if (i == 1)
            {
                zhuangtai = "开";
            }
            if (j == 0)
            {
                sb.Append("<tr class='table_td'>");
                j++;
            }
            else
            {
                sb.Append("<tr class='table_td2'>");
                j--;
            }

            sb.Append("<td><input  type='checkbox' name='id'value='" + SysFun.ToTrim(dr["id"]) + "'></td>");
            sb.Append("<td align='center'>" + SysFun.ToTrim(dr["CompanyName"]) + "</td>");
            sb.Append("<td align='center'>" + SysFun.ToTrim(dr["UserCode"]) + "</td>");
            sb.Append("<td align='center'>" + zhuangtai + "</td>");
            //      sb.Append("<td>" + SysFun.ToTrim(dr["SpellingCode"]) + "</td>");
            sb.Append("</tr>");
        }
        dr.Close();

        sb.Append("</table>");

        this.DbPageFlip1.DbPage = dbPage;

        //sb.Append("<p align='right'>" + dbPage.GetPageLink("BidTableProductSelect.aspx?PG={PageNum}" + strGetPageLink) + "</p>");

        //Label lab = new Label();
        Label1.Text = sb.ToString();
        //frmMain.Controls.Add(lab);
        db.Close();
    }

    /// <summary>
    /// 开启全部投标企业报价开关
    /// </summary>
    protected void openbjall()
    {
        Db db = new Db();
        string str = "update ProjectCompanyAttribution set PriceSwitching='1' where projectid=" + projectid;
        db.Command(str);
        db.Close();
         HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
         HZ.MBSM.BLL.LogManage.RecordLoginDetail(_Account.AccountName, Request.UserHostAddress, "更新", "ProjectCompanyAttribution", str, "../Agency/PriceSwitch.aspx");

    }
/// <summary>
/// 关闭全部企业报价开关
/// </summary>
    protected void closebjall()
    {
        Db db = new Db();
        string str = "update ProjectCompanyAttribution set PriceSwitching='0' where projectid=" + projectid;
        db.Command(str);
        db.Close();
        HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
        HZ.MBSM.BLL.LogManage.RecordLoginDetail(_Account.AccountName, Request.UserHostAddress, "更新", "ProjectCompanyAttribution", str, "../Agency/PriceSwitch.aspx");

    }
    /// <summary>
    /// 开启选定企业报价开关
    /// </summary>
    protected void openbj()
    {

        if (Request.Form["id"] != null)
        {
            int intProjectId = SysFun.ToInt(Request.QueryString["id"]);
            string[] strId = Request.Form["id"].Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);

            if (strId.Length > 0)
            {
                string strSql, sql = "";
                Db db = new Db();

                foreach (string Id in strId)
                {
                    try
                    {
                        strSql = SysFun.ToTrim(Id);
                        sql = strSql + "," + sql;

                    }
                    catch { }
                }
                sql = sql.Substring(0, sql.Length - 1);
                sql = "update ProjectCompanyAttribution set PriceSwitching='1' where projectid=" + projectid + " and id in(" + sql + ")";
                db.Command(sql);
                db.Close();
                //Response.Redirect("PriceSwitch.aspx?projectid=" + projectid);
                HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
                HZ.MBSM.BLL.LogManage.RecordLoginDetail(_Account.AccountName, Request.UserHostAddress, "更新", "ProjectCompanyAttribution", sql, "../Agency/PriceSwitch.aspx");

            }
            else
            {
                Response.Write("");
            }

        }
    }
    /// <summary>
    /// 关闭选定企业报价开关
    /// </summary>
    protected void closebj()
    {
        if (Request.Form["id"] != null)
        {
            int intProjectId = SysFun.ToInt(SysFun.IsSQL(Request.QueryString["id"]));
            string[] strId = Request.Form["id"].Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);

            if (strId.Length > 0)
            {
                string strSql, sql = "";
                Db db = new Db();
                foreach (string Id in strId)
                {
                    try
                    {
                        strSql = SysFun.ToTrim(Id);
                        sql = strSql + "," + sql;

                    }
                    catch { }
                }
                sql = sql.Substring(0, sql.Length - 1);
                sql = "update ProjectCompanyAttribution set PriceSwitching='0' where projectid=" + projectid + " and id in(" + sql + ")";
                db.Command(sql);
                db.Close();
                //Response.Redirect("PriceSwitch.aspx?projectid=" + projectid);
                HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
                HZ.MBSM.BLL.LogManage.RecordLoginDetail(_Account.AccountName, Request.UserHostAddress, "更新", "ProjectCompanyAttribution", sql, "../Agency/PriceSwitch.aspx");

            }
            else
            {
                Response.Write("");
            }

        }
    }
}